package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;


/**
 * Write a description of class OrdreUI here.
 * 
 * @author Team Black
 * @version (a version number or a date)
 */
public class OrdreUI
{
    private OrdreCtr ordreCtr;
    private ProductCtr productCtr;
    private Reader reader;
    private OrdreLibrary ordreLib;
    private Ordre myOrdre;
    private PersonCtr personCtr;
    private PersonUI personUI;
    private Employee _employee;
    private ProduktUI productUI;
    private Customer customer;
    public OrdreUI()
    {

    }   
    /**
     * prints the order menu and takes an input from the user
     */
    public void StartOrdre()
    {
        boolean exit=false;
        myOrdre = new Ordre();
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            reader = new Reader();
            productCtr = new ProductCtr();
            ordreCtr = new OrdreCtr();
            personCtr = new PersonCtr();
            personUI = new PersonUI();
            _employee = null;
            productUI = new ProduktUI();
            if(choise == 1)
            {
                startSale();
            }
            else
            {
                    if(choise == 2)
                    { 
                        deleteProductFromOrdre();
                    }
                    else
                    {
                        if(choise == 3)
                        {
                            showOrdre();
                        }//end if
                        else
                        {
                            if(choise == 4)
                            {
                               deleteOrdre();
                            }//end if
                            else
                            {
                                if(choise == 5)
                                {
                                    searchForPreviousOrders();
                                }//end if
                                else
                                {
                                    if(choise == 6)
                                    {
                                        paymentMenu();
                                    }//end if
                                    else
                                    {
                                        if(choise == 7)
                                        {
                                            billingToCustomer();
                                        }//end if
                                        else
                                        {
                                            if(choise == 8)
                                            {
                                                editRentals();
                                            }//end if
                                            else
                                            {
                                                if(choise == 9)
                                                {
                                                    addEmployee();
                                                }//end if                          
                                                writeEnd();
                                                exit = true;
                                            }//end else
                                        }//end else
                                    }//end else
                                }//end else
                            }//end else
                        }//end else
                    }// end else
             }//end else
        }//end while
    }
    /**
     * prints the order menu
     */
    private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Ordre ***");
            System.out.println(" (1) Tilføj vare til ordre");
            System.out.println(" (2) Slet vare fra ordre ");
            System.out.println(" (3) Vis ordre");
            System.out.println(" (4) Slet ordre");
            System.out.println(" (5) Søg efter tidligere ordre");
            System.out.println(" (6) Betaling");
            System.out.println(" (7) Fakturering til kunde");
            System.out.println(" (8) Rediger udlejninger");
            System.out.println(" (9) Tilføj medarbejder");
            System.out.println(" (10) Tilbage");
            System.out.print("\n Tryk for at komme videre: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
    /**
     * creates en object of Order and takes inputs from the user
     */
    private void startSale()
    {
        System.out.println("          Start Salg   ");
        System.out.println();
        if( _employee != null)
        {
            System.out.println("Du bliver betjent af " + _employee.getName());
        }
        else
        {
            System.out.println("Husk at vælge ekspedient");
            System.out.println();
        }
        if(myOrdre == null)
        {
        sellProduct();
        }
        else
        {
        System.out.println();
        boolean exit=false;
        
        while (!exit) //! means while exit not is tru4 (that is: false)
        {
            int choise = addMoreProduct();
            if(choise == 1)
            {
                sellProduct();
            }//end if
            else
            { 
                writeEnd();
                exit = true;
            }//end else
        }// end while
        }//end menu
        if(myOrdre != null)
        {
        System.out.println(" Nu betjenes ordre " + myOrdre.getOrderId() );
        }
        
    }
    /**
     * takes an input from the user and deletes a product from the order
     */
    private void deleteProductFromOrdre()
    {
        System.out.println();
        System.out.print("   Slet vare fra Ordre    ");
        System.out.println();
        Product currproduct = null;
        boolean exit = false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = SearchdeleteProduct();
            if(choise == 1)
            {
                int choises = productUI.writeSearchMenu();
                boolean exits = false;
                while (!exits) //! means while exit not is true (that is: false)
                {
                    if(choises == 1)
                    {
                        String _name = reader.inputName();
                        currproduct = productCtr.searchProductByName(_name);
                        System.out.println("varen er valgt");
                    }//end if
                    else
                    {
                        if(choises == 2)
                        {
                            int _id = reader.inputProductId();
                            currproduct = productCtr.searchProductById(_id);
                            System.out.println("varen er valgt");
                        }//end if
                        else
                        {
                            writeEnd();
                            exit = true;
                        }//end else
                    }//end else
                }//end while "exits"
            }//end if
            else
            {
                if(choise == 2)
                {
                    int _id = reader.inputProductId();
                    currproduct = productCtr.searchProductById(_id);
                }//end if
                else
                {
                    if(choise == 3)
                    {
                        String _name = reader.inputProductName();
                        currproduct = productCtr.searchProductByName(_name);
                    }
                }//end else
                writeEnd();
                exit = true;     
            }
            PartOrdre temPartOrdre;
            temPartOrdre = myOrdre.searchPartOrdreByProduct(currproduct);
            System.out.println();
            boolean exitt=false;
            while (!exitt) //! means while exit not is true (that is: false)
            {
                 int choisees = deleteordremenu();
                 if(choisees == 1)
                 {
                     myOrdre.removePartOrdre(temPartOrdre);
                     System.out.println("Denne del ordre er nu slettet");
                 }//end if
                    else
                    {
                        if(choisees == 2)
                        {
                            deleteProductFromOrdre();
                        }//end if
                        writeEnd();
                        exitt = true;
                    }//end else
               }// end while
        }
    }
    /**
     * prints the information about the order
     */
    private void showOrdre()
    {
        System.out.println();
        
        System.out.println("     Info om en Ordre     ");
        System.out.println("Indtast ordre nummeret");
        int _id = reader.inputId();
        Ordre temOrdre = ordreLib.searchOrdreById(_id);

        Customer currCustomer = temOrdre.getCustomer();
        Employee curremployee = temOrdre.getEmployee();
        
        System.out.println(" Ordre nr:                  " + temOrdre.getOrderId());
        System.out.println(" Status på ordren:          " + temOrdre.getStatus());
        System.out.println(" Total pris for ordren:     " + temOrdre.getTotal());
        System.out.println();
        System.out.println(" Kundens navn:              " + currCustomer.getName());
        System.out.println(" Kundens telefon nr:        " + currCustomer.getPhoneNr());
        System.out.println(" Kundens adresse:           " + currCustomer.getAdress());
        System.out.println(" Kundens post nummer:       " + currCustomer.getZipCode());
        System.out.println(" Kundens rabat i %          " + currCustomer.getDiscount());
        System.out.println();
        System.out.println(" Ekspedienten navn:         " + currCustomer.getName());
        System.out.println(" Ekspedienten telefon nr:   " + currCustomer.getPhoneNr());
        System.out.println(" Ekspedienten adresse:      " + currCustomer.getAdress());
        System.out.println(" Ekspedienten post nummer:  " + currCustomer.getZipCode());
        System.out.println(" Ekspedienten rabat i %     " + currCustomer.getDiscount());
    }
    /**
     * takes an input from the user and deletes an object of Order
     */
    private void deleteOrdre()
    {
        System.out.println();
        System.out.println("Indtast ordre ID: ");
        
        int _orderId;
        _orderId = reader.inputId(); 
        Ordre delOrdre = ordreCtr.getOrdreById(_orderId);
       
        ordreLib.removeOrdre(delOrdre);
        
    }
    /**
     * under construction
     */
    private void searchForPreviousOrders()
    {
        System.out.println();
        System.out.println("Under konstruktion");
    }
    /**
     * takes an input from the user and prints a message about payment
     */
    private void payment()
    {
        System.out.println();
        boolean exit=false;
        
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            if(choise == 1)
            {
                //ordre(_id).getPrice
            }//end if
            else{
                    if(choise == 2)
                    { 
                       System.out.println("Gå til terminal");
                    }//end if
                    else{
                        if(choise == 3)
                        {
                            System.out.println("Chekken skal udskrives til: Vestbjerg Byggecenter A/S");
                            System.out.println("Den må kun udskrives lige på beløbet");
                            System.out.println("total: " + myOrdre.getTotal());
                        }//end if
                        else
                        {
                            writeEnd();
                            exit = true;
                        }//end else
                    }// end else
                }//end else
            }//end while
            //Videre videreudvikling er nødvendigt
    }
    /**
     * takes an input from the user and prints info about a customer's billing status
     * under construction - billing to be added to an object of Customer
     */
    private void billingToCustomer()
    {
        System.out.println();
        double _totalPrice = myOrdre.getTotal();
        System.out.println("send regning til kunde ud fra navn");
        String _name = reader.inputName();
        customer = personCtr.searchCustomerByName(_name);
        System.out.println("Total prisen for denne ordre: " + _totalPrice + " kr.");
        
        System.out.println(customer.getName() + " har " + _totalPrice + " til at stå på regningen" );
        System.out.println("Ring til: " + customer.getPhoneNr());
        System.out.println("auto mail svar under udarbejdelse");
    }
    
    /**
     * under construction
     */
    private void editRentals()
    {
        System.out.println();
        System.out.println("Under konstruktion");
        //Videre videreudvikling er nødvendigt
    }
    /**
     * takes an input from the user and adds an object of Employee to the order
     */
    private void addEmployee()
    {
        System.out.println();
        System.out.println("Vælg ansat");
        boolean exitA = false;
        int choise = personUI.writeSearchMenu();
        while(!exitA)
        {
            //int choise = personUI.writeSearchMenu();
            if(choise == 1)
            {
                String _name = reader.inputName();
                _employee  = personCtr.searchEmployeeByName(_name);
                System.out.println("Du har nu tilføjet " + _employee.getName() + " til denne ordre.");
                pause();
                exitA = true;
            }
            else
            {
                if(choise == 2)
                {
                    int _id = reader.inputId();
                    _employee  = personCtr.searchEmployeeById(_id);
                    System.out.println("Du har nu tilføjet " + _employee.getId() + " til denne ordre.");
                    pause();
                    exitA = true;
                }
                else
                {
                    if(choise == 3)
                    {
                        exitA = true;
                    }
                }
            }
        }
        myOrdre.setEmployee(_employee);
                
    }
    /**
    * prints an end message
    */
    private void writeEnd()
    {
        System.out.println(" Systemet slukker ");
    }
    /**
    * takes an input from the user
    */
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
    /**
     * takes an input from the user and prints info about payment methods
     */
    private int paymentMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Hvilket betaling middel ønsker de at gøre brug af?");
        System.out.println(" (1) Kontant");
        System.out.println(" (2) Dankort");
        System.out.println(" (3) Check");
        System.out.println(" (4) Tilbage");
        
        int choise = keyboard.nextInt();  
        return choise;
    }
    /**
     * makes a new object of Order, takes an input from the user and adds a partorder to the order
     */
    public void sellProduct()
    {   
        myOrdre = ordreCtr.createOrder();
        
        
        int  _id = reader.inputProductId();
        Product _product = productCtr.searchProductById(_id);
        
        PartOrdre _partOrder =  ordreCtr.createPartOrder(_product);
        myOrdre.addPartOrdre(_partOrder);  
        
        System.out.println(" Pris: " + myOrdre.getTotal());
        pause();
       
    }
    /**
     * takes an input from the user and prints a message about additional products
     */
    public int addMoreProduct()
    {
       Scanner keyboard = new Scanner(System.in); 
       
       System.out.println("Tryk 1 for at vende tilbage til vareindtastning ");
       System.out.println("Tryk 2 for at gå til hovedmenu");
        
       int choise = keyboard.nextInt();
       return choise; 
    }
    /**
     * takes an input from the user and deletes a product from the order
     */
    public int SearchdeleteProduct()
    {
       Scanner keyboard = new Scanner(System.in); 
       
       System.out.println("Tryk 1 for at søge efter vare ");
       System.out.println("Tryk 2 for at indtaste ID på varen");
       System.out.println("Tryk 3 for at indtaste navn på varen");
       System.out.println("Tryk 4 for at gå tilbage");        
       int choise = keyboard.nextInt();
       return choise; 
    }
    /**
     * takes an input from the user and deletes an order
     */
    public int deleteordremenu()
    {
       Scanner keyboard = new Scanner(System.in); 
       
       System.out.println("Er du helt sikker på du vil slettet denne ordre?");
       System.out.println();    
       System.out.println("Tryk 1 for at bekræft sletning ");
       System.out.println("Tryk 2 for at vælge en anden vare");
       System.out.println("Tryk 3 for at afbryde");        
      
       int choise = keyboard.nextInt();
       return choise; 
    }

}
    